
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>Mua~亲亲宠————我的订单</title>
<link rel="stylesheet" href="css/style.css" />

<link rel="stylesheet" href="css/bootstrap.css" />
<link rel="stylesheet" href="css/bootstrap-table.min.css" />

<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/bootstrap.min.js"></script>

<script type="text/javascript" src="js/bootstrap-table.min.js"></script>

<script type="text/javascript" src="js/bootstrap-table-zh-CN.min.js"></script>
</head>

<body>
	<%@ include file="/WEB-INF/jsp/top.jspf"%>
	<div class="title"></div>
	<div class="col-sm-offset-1 col-sm-10">
		<div class="page-header" style="color:#000099">
			<h3>我的订单</h3>
		</div>
		<div class="container  col-sm-offset-1  col-sm-10">
			<table id="myOrderTable" class="table table-striped">

			</table>
		</div>
	</div>
	<script type="text/javascript">
		var userId;
		$('#myOrderTable')
				.bootstrapTable(
						{
							url : 'order/listMyOrder.do',
							pagination : true, // 是否显示分页（*）
							sidePagination : "client", // 分页方式：client客户端分页，server服务端分页（*）
							pageNumber : 1, // 初始化加载第一页，默认第一页
							pageSize : 10, // 每页的记录行数（*）
							pageList : [ 5, 10, 25 ], // 可供选择的每页的行数（*）
							detailView : true, //是否显示详细视图
							detailFormatter : function(index, row) {
								var html = [];
								html.push('<p><b> 订单号 :</b> ' + row.orderNo
										+ '</p>');
								html
										.push('<p><b> 宠物 :</b> '
												+ row.petTitle
												+ '<b> 价格:</b> '
												+ row.price
												+ '<button class="btn btn-sm  btn-info" onclick="findPet('
												+ row.petId
												+ ')">查看详情</button></p>');
								html
										.push('<p><b> 卖家 :</b> '
												+ row.sellerName
												+ '<button class="btn btn-sm  btn-info" onclick="watchUser('
												+ row.sellerId
												+ ')">查看详情</button></p>');
								html
										.push('<p><b> 买家 :</b> '
												+ row.buyerName
												+ '<button class="btn btn-sm  btn-info" onclick="watchUser('
												+ row.buyerId
												+ ')">查看详情</button></p>');
								html.push('<b> 生成时间:</b> '
										+ new Date(row.createDate)
												.toLocaleDateString());
								if (row.dealDate != null) {
									html.push('<b> 完成时间:</b> '
											+ new Date(row.dealDate)
													.toLocaleDateString());

								}

								return html.join('');
							},
							responseHandler : function(res) {
								userId = res.userId;
								return res.orderList;
							},
							columns : [
									{
										title : '序号',
										field : 'no',
										align : 'center',
										valign : 'middle',
										formatter : function(value, row, index) {
											return index + 1;
										}
									},
									{
										title : '订单号号',
										field : 'orderNo',
										align : 'center',
										valign : 'middle'
									},
									{
										title : '标题',
										field : 'petTitle',
										align : 'center',
										valign : 'middle'
									},
									{
										title : '价格',
										field : 'price',
										align : 'center',
										valign : 'middle'
									},
									{
										title : '生成时间',
										field : 'createDate',
										align : 'center',
										valign : 'middle',
										formatter : function(value) {
											if (!isNaN(value)) {
												var Timestamp = new Date(value);
												return Timestamp
														.toLocaleDateString();
											}
										}
									},
									{
										title : '关系',
										field : 'buyerId',
										align : 'center',
										valign : 'middle',
										formatter : function(value) {
											var str = "卖出";
											if (userId == value) {
												str = "买入";
											}
											return str;
										}
									},
									{
										title : '状态',
										field : 'state',
										align : 'center',
										formatter : function(value, row) {
											var paytext = "待付款";
											var paybtn = '<a href="order/payOrder.do?id='
													+ row.id
													+ '"><button class="btn btn-sm  btn-info">前往付款</button></a> ';
											var sendtext = "待发货";
											var sendbtn = '<a href="order/sendOrder.do?id='
													+ row.id
													+ '"><button class="btn btn-sm  btn-info">前往发货</button></a> ';
											var receivetext = "待收货";
											var receivebtn = '<button class="btn btn-sm  btn-info" onclick="receive('
													+ row.id
													+ ')">确认收货</button>';
											var donetext = "已完成";
											if (value == 0) {
												if (userId == row.buyerId) {
													return paybtn;
												} else {
													return paytext;
												}
											} else if (value == 1) {
												if (userId == row.buyerId) {
													return sendtext;
												} else {
													return sendbtn;
												}
											} else if (value == 2) {
												if (userId == row.buyerId) {
													return receivebtn;
												} else {
													return receivetext;
												}
											} else {
												return donetext;
											}
										}
									} ]
						});
		function findPet(pet_id) {
			window.open("pet/showPetDetail.do?id=" + pet_id);
		}
		function watchUser(user_id) {
			window.open("user/userInfo.do?userId=" + user_id);
		}
		function receive(id) {
			$.ajax({
				url : "order/receive.do",
				data : {
					"orderId" : id
				},
				type : "POST",
				success : function(result) {
					//请求成功时
					if (result.msg == 'success') {
						$(".modal-body").html("收货成功！点击确定前往我的订单");
						$("#topModal").modal("show");
						$("#topModal").on('hidden.bs.modal', function() {
							var gotoURL = "order/myOrders.do";
							window.location.href = gotoURL;
						});
					} else if (result.msg == 'error') {
						$(".modal-body").html(result.errorMsg);
						$("#topModal").modal("show");
					}
				},
				error : function() {
					//请求失败时
					$(".modal-body").html("收货失败");
					$("#topModal").modal("show");
				}
			});
		}
	</script>
</body>
</html>
